package com.service.dialtest.controller;

import com.service.dialtest.dto.CloudUserDto;
import com.service.dialtest.service.CloudUserService;
import com.service.dialtest.service.DialTestService;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.service.config.constant.Constant.*;

/**
 * @Description:
 * @Author: hjl
 * @Date: 2021/10/28 14:52
 */
@RestController
@CrossOrigin
@Api("拨测接口")
@RequestMapping("/dialTest")
public class DialTestController {
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private DialTestService dialTestService;
    @Autowired
    private CloudUserService cloudUserService;

    @GetMapping("getDataOfDialTest")
    @ApiOperation(value = "获取拨测数据")
    @ResponseBody
    public Result queryDayaOfDialTest(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                List<CloudUserDto> cloudUserDtoList = cloudUserService.queryAll(userId);
                if (cloudUserDtoList == null || cloudUserDtoList.size()==0 ) {
                    return Result.failure(10013,"您的账号还未关联华为云管理账号，请进入大屏后台进行关联");
                }
                return Result.ok().setData(dialTestService.queryDayaOfDialTest(userId));
            } else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
